#coding:utf8
from help import * 
pause_str = 'scroll'
scroll_str = 'pause'

#config
alt_fix_y = True
dst_arr = 'aero_pin_xl.cur'
move_task_delay1 = 0.1
move_task_delay2 = 0.18
#微调角度速率配置，可添加
is_working = False
sca_l = [1, 0.3]
max_zi = 60 #最多数字坐标个数
max_l = 3000 #最大数字坐标长度
main_win_size =  (50, 24)  #提示窗口大小，高度是25时，文字显示会上下动。
cir_para = [17, 20, 3, 1, -15, 4]  #鼠标附近提示圆点。
screen_start = [[0,0], [-1366,291] ]
screen_size = [[1366,768],[1366,768] ]
ge_m = {
    0:[0,0],
    1:[400,8],
    2:[800,16],
    3:[1200,24],
    4:[1600,32],    
}
ge_m_default = [3000,40]
ini_pos = [1200,50]

if is_working:
    ini_pos = [2300,50]
    sca_l = [1, 0.3, 0.1]
    max_zi = 80   
    max_l = 5000    
    main_win_size =  (50, 26)
    cir_para = [18, 23, 3, 1, -15, 4]
    screen_start = [[0,0], [2560,-586] ]
    screen_size = [[2560,1440],[1440,2560] ]
    ge_m = {
    0:[0,0],
    1:[400,8],
    2:[800,16],
    3:[1200,24],
    4:[1600,32],    
    }
    ge_m_default = [3000,40]
    
# 设置初始长度相关
ge_wait_dis = 10
def ge_add_xing(a):
    cfg.min_ge_has_refresh = False
    cfg.min_ge = a+1

def ge_add_x2(a):
    cfg.min_ge_has_refresh = False
    if a==0:
        cfg.min_ge = 2
        return
    cfg.min_ge = a+1

def ge_add_ctrl(a):    
    cfg.min_ge_has_refresh = False
    cfg.min_ge = a+2

def get_ge():
    return ge_m.get(cfg.min_ge,ge_m_default)

screen_gap = 5
screen_config_for_indx = []
screen_config = []
for i in range(len(screen_start)):
    x,y = screen_start[i]
    w,h = screen_size[i]
    screen_config_for_indx.append([x,y,x+w-1,y+h-1] )
    screen_config.append([x,y,x+w-1-screen_gap,y+h-1-screen_gap] )

enter_gap = 80 #not show  enter方向组合键，坐标间距
enter_ge = 150  #enter方向组合键，显示坐标长度
enter_gap2 = 180 # +方向组合键，坐标间距
xing_ge = 6
to_lap = 0.3
show_arr_time = 0.18
keep_alive_time = 600

du_refresh_interval = 0.05

lock_l = [1, 12, 60] #单步移动模式速度，可添加。第一个固定为1
lock_step_default = 1 #默认移动速度，是上面列表的位置序号

min_d = 25 #数字坐标间最小距离
min_d_mouse = 30 #数字坐标距离鼠标箭头最小距离
icon_file_path = 'aero_ns_xl.cur' #如果运行目录下有此icon文件，会显示系统托盘
sum_max_l = 6 #倍数数字最大长度
draw_txt_yAdd = 10 # 画数字坐标时，文字上下偏移
draw_txt_txtSize = 16  #数字坐标数字大小
draw_txt_pathSize = 6   #数字坐标边缘大小
draw_txt_pathSize2 = 8   #数字坐标边缘大小
draw_cir_out_width = 3 #圆圈外圈宽度
draw_cir_out_rad = 8 #圆圈外圈半径
draw_cir_in_width = 3 #圆圈内圈宽度
draw_cir_in_rad = 7 #圆圈内圈半径
txt_win_size = (60, 30) #数字坐标窗口大小
txt_gap = 10  #数字坐标在窗口中的偏移
cir_win_size = (60, 30) #圆圈窗口大小
cir_gap = 10 #圆圈在窗口中的偏移

main_win_border = 1 # 提示窗口边框大小
main_win_txt_size = 10 #提示窗口文字大小
small_win_trans = True #数字坐标窗口和圆圈窗口是否透明，试验大小时可以改成false
pos_json_path = 'pos.json' #点位坐标文件路径
mouse_delay = 0.3
two_mouse_check_interval = 0.4
resize_cycle = 100

def cal_random(co):
    l = [[1, 1], [1, -1], [-1, 1], [-1, -1]]
    x, y = l[co%4]
    d = 10
    x = d * x
    y = d * y
    a = random.randint(0, 9999)
    t = 2 * math.pi * a / 10000
    dx, dy = math.cos(t), math.sin(t)    
    g = random.randint(10, 20)
    return dx * g, dy * g

def can_jump():
    if cfg.isShow or cfg.show_one_on:
        return False
    return True

def check_jump():
    time.sleep(0.05)
    return  time.time() - cfg.left_mouse_time < 0.05 and can_jump() and time.time() - cfg.right_mouse_time < 0.3
            
def get_x2_d_sca():
    d = cal_dis ((cfg.snap[-1][1], cfg.snap[-1][2]), (cfg.snap[0][1], cfg.snap[0][2]))
    if d < 400:
        return 2.5
    if d < 1300:
        return 1.5
    if d < 2000:
        return 1
    return 0.7    

def get_near_count(d):    
    if d > 150:
        return 2,2
    if d > 100:
        return 2,2
    return 2,2

def get_far_count(d,en,st):
    if en-st<=5:
        return 2
    if en-st<8:
        return 3
    if d < 70:
        return 4
    return 3

def add_idx(en,st, d,last):    
    if st-en==2:
        return en+1,1    
    # if st - en ==5:
    #     x = (st+en+1)//2-1
    #     return x,2             

    if st - en >=3:
        if (st+en)%2==0:
            x = (st+en)//2-1
            if last>x:
                return x+1,2
            return x,2
        
        x = (st+en+1)//2-1
        if last>x:
            return x+1,2
        if last<x:
            return x-1,2        
        return x,2             
    return 0, 0

def get_scroll_sca():
    if cfg.to<600:
        return 200
    if cfg.to<1000:
        return 250
    return 300

def get_suo_di():
    if cfg.to<300:
        return 20
    if cfg.to<1000:
        return 25
    return 30

def get_to_sca():
    if cfg.to < 100:
        return 15
    if cfg.to<300:
        return 20
    if cfg.to<1000:
        return 25
    return 30

def clear_sca():    
    cfg.sca = 0
    cfg.sca_du = 0
    cfg.sca_cur = 0
    
def lock_step_clear():    
    cfg.lock_step = lock_step_default
    
def clear_lock():    
    cfg.lock = False
    lock_step_clear()
    cfg.lock_xing_mode = False
    cfg.lock_jia_mode = False

def get_cur_pos():
    a = mouse.Controller().position
    if type(a) != type((0, 0)):
        return cfg.fix_pos
    if len(a) != 2:
        return cfg.fix_pos
    return a

def set_to_chg():
    cfg.to_change_time = time.time()
    cfg.add_show=set()

import traceback
def set_cfg_sum_0():
    #traceback.print_stack()  
    print('sum to 0')
    cfg.sum = '0'

def clear(pos, clearLock=True):
    cfg.isShow = False
    cfg.to_time = 0
    cfg.to_change_time = 0
    cfg.add_show=set()
    cfg.showCut = True    
    cfg.w2_last_ge = 0
    cfg.w2_last_to = 0
    cfg.w2_last_dis = 0
    cfg.last_ge = 0
    cfg.w2_min_ge = 0    
    cfg.to = 0
    cfg.force_scroll = False
    set_cfg_sum_0()
    clear_sca()
    cfg.force_show = False
    cfg.mouse_mode = 0
    cfg.mouse_m_int_stop = (0,0)
    cfg.mouse_m_dst_stop = (0,0)
    cfg.mouse_m_left_start_time = 0
    cfg.mouse_m_left_dis = [0, 0]
    cfg.mouse_m_right_dis = [0, 0]
    cfg.mouse_m_left_fix_time = 0
    cfg.mouse_m_left_fix_pos = [0, 0]    
    cfg.mouse_m_left_max = [0, 0]
    cfg.clear_time = time.time()
    cfg.fix_scroll = 0    
    cfg.manu_add = 0    
    cfg.min_ge = 0
    cfg.min_ge_has_refresh = False
    cfg.min_ge_force_show = False
    cfg.min_ge_force_not_show = False
    cfg.scroll_one = 0
    
    if clearLock:
        clear_lock()
    if pos:
        cfg.fix_pos = get_cur_pos()

class Cg():
    def __init__(self) -> None:
        self.scroll_one = 0
        self.min_ge_force_not_show = False
        self.min_ge_has_refresh = False
        self.min_ge_force_show = False        
        self.min_ge = 0
        self.to_exit = False
        self.alt_mode = 0
        self.ctrl_ori_lock = False
        self.manu_add = 0
        self.add_show=set()        
        self.invert_x = True #正在invert的方向
        self.invert_other_len =0 #非invert方向合计长度
        self.last_m = (0,0)
        self.last_active_time = 0
        self.pause_mode = True
        self.maxi = 0
        self.fix_scroll = 0
        self.clear_time = 0
        self.mouse_mode = 0
        self.mouse_mode4_pos = (0,0)
        self.mouse_mode4_right_time = 0
        self.mouse_m_left_start_time = 0        
        self.mouse_m_left_dis = [0, 0]
        self.mouse_m_right_dis = [0, 0]
        self.mouse_m_left_fix_time = 0
        self.mouse_m_left_fix_pos = [0, 0]
        self.mouse_m_left_max = [0, 0]
        
        self.x2_mode = False
        self.x2_mode_start_pos = (0,0)
        self.x2_mode_d = 100
        self.x2_mode_dst = [0, 0]
        
        self.w2_last_dis = 0 # to 不变时，到fix_pos 的距离
        self.w2_last_to = 0
        self.w2_last_ge=0
        self.w2_min_ge=0
        
        self.last_ge = 0  # 上次显示的坐标图标个数,不包括0
        
        self.isOn2 = False #isOn2如果是false，鼠标点击后不开启显示模式；是true，点击后开启显示模式。
        self.to = 0
        self.to_time = 0        
        self.always_show_one = True
        self.reset_show_one = False
        self.force_show = False
        self.fix_pos = (0, 0)  #起始位置
        self.isShow = False #是否显示
        self.showCut = True
        self.sum = '0' #倍数
        self.chu_pressed = False
        self.chu_pressed_then_other = False

        self.move_task = None #sum不为0，移动鼠标，产生移动任务
        self.move_task_time = 0
        self.last_move_time = 0  #上次鼠标移动的时间。检测鼠标移动停顿，再执行移动任务。
        self.click_time = -1 #点击任务
        self.set_fix_time = 0 #移动后，设置起始位置，并设置此时间。用来处理移动出屏幕范围的情况。
        self.is_pressed = False  #左键是否按下
        self.last_mouse_pos = (0, 0)  #键盘操作时鼠标的位置。移动任务需要。
        self.ctrl_pressed = False  # ctrl键按下
        self.ctrl_pressed_any_other_key = False 
        self.ctrl_pressed_then_other = False
        self.alt_pressed= False  #alt键按下
        self.alt_pressed_then_other =False
        self.ori_xing_mode = False
        self.lock = False #数字键盘用来单步移动。
        self.lock_xing_mode = False
        self.lock_jia_mode = False
        self.lock_xing_last_move = 4
        self.force_scroll = False
        self.left_mouse = []
        self.right_mouse = []
        self.lock_step = lock_step_default #单步移动的速度。
        self.sca = 0  #微调角度的比例
        self.sca_du = 0 #微调角度上次剩余的度数
        self.sca_cur = 0 #微调角度固定点的度数
        self.sca_cur_pos = (0, 0) #微调角度固定点的位置
        self.pause_press_time = 0 #pause键按下的时间，用来判断双击
        self.pause_key = True #pause按下变换此值，用来隐藏窗口。
        self.pause_move = None #双击pause键，产生移动任务
        self.ctrl_l_time = 0 #左ctrl按下时间，用来判断双击。
        self.pause_pressed = False # pause 按住
        self.pause_pressed_then_other = False  #pause按住组合键       
        self.add_pressed = False
        self.add_pressed_then_other = False 
        self.real_mouse_time = 0
        self.is_suo = False
        self.suo_dis = 0
        self.suo_to = 0
        self.to_change_time = 0
        self.x2_pressed = False
        self.x2_pressed_then_other =False
        self.mouse_pressed = False
        self.move_to_win = False
        self.show_one_onetime = False
        self.enter_pressed = False
        self.enter_pressed_then_other = False
        self.cmd_mode =False         
        self.set_mode = False
        self.pos_m = {}
        self.key_input_time = 0
        self.show_one_on = self.always_show_one
        self.show_mouse_tip = True
        self.show_mouse_tip2 = True
        self.w2showing = False
        self.w2snap = []
        self.snap = []
        self.left_mouse_time = 0    
        self.right_mouse_time = 0            
cfg = Cg()
